package com.zsxb.crm.controller;

import com.zsxb.crm.annotation.Log;
import com.zsxb.crm.dto.BusinessDTO;
import com.zsxb.crm.dto.BusinessPoolDTO;
import com.zsxb.crm.dto.BusinessRecordDTO;
import com.zsxb.crm.dto.ClueGainDTO;
import com.zsxb.crm.pojo.Business;
import com.zsxb.crm.result.PageResult;
import com.zsxb.crm.result.Result;
import com.zsxb.crm.service.BusinessService;
import com.zsxb.crm.vo.RecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business")
@Slf4j
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    //商机分页查询
    @GetMapping("/list")
    public PageResult pageQuery(BusinessDTO businessDTO) {
        log.info("商机分页查询参数:{}", businessDTO);
        PageResult pageResult = businessService.pageQuery(businessDTO);
        return PageResult.success(pageResult);
    }

    @GetMapping("/pool")
    public PageResult poolPageQuery(BusinessPoolDTO businessPoolDTO){
        PageResult pageResult = businessService.poolPageQuery(businessPoolDTO);
        return PageResult.success(pageResult);
    }

    //根据id查询商机
    @GetMapping("/record/list")
    public Result getByRecordId(Long businessId) {
        log.info("根据id查询商机：{}", businessId);
        List<RecordVO> list = businessService.findRecordByBusinessId(businessId);
        return Result.success(list);
    }


    //根据id查询商机
    @GetMapping("/{businessId}")
    public Result getById(@PathVariable Long businessId) {
        log.info("根据id查询商机：{}", businessId);
        Business business = businessService.getById(businessId);
        return Result.success(business);
    }

    //退回公海
    @PutMapping("/back/{businessId}/{reasonValue}")
    @Log
    public Result backSea(@PathVariable Long businessId,@PathVariable Integer reasonValue){
        businessService.backSea(businessId,reasonValue);
        return Result.success();
    }


    @PostMapping
    @Log
    public Result<String> addBusiness(@RequestBody Business business) {
        log.debug("新增商机信息:{}", business);
        businessService.addBusiness(business);
        return Result.success();
    }

    @PostMapping("/record")
    @Log
    public Result<String> addBusinessDetail(@RequestBody BusinessRecordDTO businessRecordDTO){
        businessService.addBusinessDetail(businessRecordDTO);
        return Result.success();
    }
    //分配商机
    @PutMapping("/assignment")
    @Log
    public Result assign(@RequestBody ClueGainDTO clueGainDTO) {
        log.debug("分配商机信息:{}", clueGainDTO);
        businessService.assign(clueGainDTO);
        return Result.success();
    }

    @PutMapping("/gain")
    @Log
    public Result<String> gainBusiness(@RequestBody ClueGainDTO clueGainDTO){
        businessService.gainBusiness(clueGainDTO);
        return Result.success();
    }
}
